home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / allocwg.com / REDUCEAL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-10  |  2.6 KB  |  80 lines

  1. #ifndef MSC
  2.  
  3.     /*  Non-MSC Version  */
  4.  
  5. #if (defined(M_I86CM) || defined(M_I86LM) || defined(M_I86HM))
  6.  
  7.     /*  NOTE:  These procedures should only be used for
  8.                large data model programs (i.e., Compact, Large, Huge) */
  9.  
  10. #include <stddef.h>
  11. #include <dos.h>
  12. #include "alloc.h"
  13.  
  14. extern  ATABLE  AllocationTable [] ; /* Provide storage for block pointers */
  15. extern  SUNIT   NEntry             ; /* Number of table entries            */
  16.  
  17. void ReduceAllocation ( CompressLastBlock )
  18.  
  19.    int    CompressLastBlock  ;  /*  Compress last block flag  */
  20.  
  21. /*
  22.          +---------------------------------------+
  23.          |                                       |  
  24.          |  This procedure attempts to reduce    |  
  25.          |  the current memory allocation to the |  
  26.          |  minimum required by freeing segments |  
  27.          |  which are entirely empty back to     |  
  28.          |  the system.                          |  
  29.          |                                       |  
  30.          |  If 'CompressLastBlock' flag is set,  |  
  31.          |  the memory allocation for the last   |  
  32.          |  block is reduced to the minimum      |  
  33.          |  (i.e., maximum compression).         |  
  34.          |                                       |  
  35.          +---------------------------------------+
  36. */
  37.  
  38. {
  39.    HEADER *Header ;
  40.    SUNIT  *bptr   ;
  41.    SUNIT   offset ;
  42.    SUNIT   noffset;
  43.    SUNIT   bsize  ;
  44.    int     i      ;
  45.    void   CollapseFreeBlocks () ;
  46.    void   FreeEmptyBlocks    () ;
  47.     
  48.  
  49.    if ( NEntry > 0 ) {
  50.       for ( i = 0 ; i < NEntry ; i++ )
  51.          CollapseFreeBlocks ( AllocationTable [i].Header ) ;
  52.       FreeEmptyBlocks ( AllocationTable [NEntry-1].Header ) ;
  53.       if ( NEntry > 0 && CompressLastBlock ) {
  54.  
  55.          /*  Find last block in allocation  */
  56.          
  57.          Header  = AllocationTable [NEntry-1].Header ;
  58.          noffset = HSIZE ;
  59.          while ( noffset < Header->BytesUsed ) {
  60.             offset   = noffset ;
  61.             bptr     = (SUNIT *) Header + offset/SSIZE ;
  62.             bsize    = *bptr & ~FREE ;
  63.             noffset += bsize + SSIZE ;
  64.          } ;
  65.          if ( (*bptr & FREE ) && ( bsize + SSIZE ) >= NALLOC ) {
  66.             bsize    = Header->BytesUsed - bsize - SSIZE ;
  67.             noffset  = NALLOC *( (bsize-1)/NALLOC+1) ;
  68.             if (_dos_setblock ( noffset/NALLOC, FP_SEG (Header), &i ) != 0 )
  69.                return ;
  70.             if ( bsize < noffset )
  71.                *bptr = ( noffset - bsize - SSIZE ) | FREE ;
  72.             Header->BytesUsed = noffset ;
  73.             AllocationTable [NEntry-1].Size = noffset ;
  74.          } ;
  75.       } ;
  76.    } ;         
  77. }
  78. #endif
  79. #endif
  80.